<?php

class user extends adminControl
{
	protected function indexAction()
	{
		$pagesize = 20;
		$page = $_GET['page'] >=1 ? intval($_GET['page']) : 1;
		$start = ($page - 1)*$pagesize;
		
		if (isset($_GET['groupid']))
		{
			$where = ' WHERE groupid = ' . intval($_GET['groupid']);
		}

		$rs = $this->db->get_one("SELECT COUNT(*) AS total FROM cms_user $where");
		$count = $rs['total'];
		$numofpage = ceil($count/$pagesize);

		$pages = numofpage($count, $page, $numofpage, "$this->basename&");

		$rs = $this->db->query("SELECT * FROM cms_user $where  ORDER BY uid LIMIT $start, $pagesize");
		$admindb = array();

		include_once(R_P . 'data/cache/group.php');
		while ($admins = $this->db->fetch_array($rs))
		{
			$admins['logintime']=$admins['logintime'] ? get_date($admins['logintime']) : '--';
			$admins['group'] = empty($admins['groupid']) ? '--' : $group[$admins['groupid']]['groupname'];
			empty($admins['ip']) && $admins['ip']='--';
			empty($admins['email']) && $admins['email']='--';
			
			$admins['priv']=explode($admins['priv']);
			$admindb[]=$admins;
		}

		$this->fillData('count', $count);
		$this->fillData('pages', $pages);
		$this->fillData('admindb', $admindb);
		$this->fillData('select', $this->getGroup());
		//echo $this->getGroup();exit;
		$this->display('user');
	}

	protected function getAddAction()
	{
		$this->fillData('select', $this->getGroup());
		$this->display('user');
	}

	protected function postAddAction()
	{
		$new_username = Char_cv($_POST['new_username']);
		$new_password = trim($_POST['new_password']);
		$new_email = trim($_POST['new_email']);
		$new_password2 = trim($_POST['new_password2']);
		$groupid = intval($_POST['groupid']);
		$realname = Char_cv($_POST['realname']);
		$idcard = Char_cv($_POST['idcard']);
		$telephone = Char_cv($_POST['telephone']);
		$ifadmin = intval($_POST['ifadmin']);

		empty($new_username) && Showmsg('admin_nousername');
		empty($new_password) && Showmsg('admin_nopassword');
		$new_password != $new_password2 && Showmsg('set_confirmpwd');

		$rs = $this->db->get_one("SELECT * FROM cms_user WHERE username='$new_username'");
		$rs && Showmsg('admin_userexists');
		$new_password = md5($new_password);

		$this->db->update("INSERT INTO cms_user SET
			`username`='$new_username',
			`password`='$new_password',
			`email`='$new_email',
			`groupid`='$groupid',
			`telephone`='$telephone',
			`idcard`='$idcard',
			`realname`='$realname',
			`ifadmin`='$ifadmin'
		");
		adminmsg('用户创建成功');
	}

	protected function getEditAction()
	{
		$uid = intval($_GET['uid']);
		$edit = $this->db->get_one("SELECT * FROM cms_user WHERE uid='$uid'");
		ifcheck($edit['ifadmin'], 'ifadmin');
		$this->fillData('edit', $edit);
		$this->fillData('ifadmin_Y', $GLOBALS['ifadmin_Y']);
		$this->fillData('ifadmin_N', $GLOBALS['ifadmin_N']);
		$this->fillData('select', $this->getGroup($edit['groupid']));
		$this->display('user');
	}

	protected function postEditAction()
	{
		$uid = intval($_POST['uid']);
				
		$new_password = trim($_POST['new_password']);
		$new_email = trim($_POST['new_email']);
		$new_password2 = trim($_POST['new_password2']);
		$groupid = intval($_POST['groupid']);
		$realname = Char_cv($_POST['realname']);
		$idcard = Char_cv($_POST['idcard']);
		$telephone = Char_cv($_POST['telephone']);
		$ifadmin = intval($_POST['ifadmin']);

		$edit = $this->db->get_one("SELECT * FROM cms_user WHERE uid='$uid'");
		if ($ifadmin == 0 && $edit['username'] == $GLOBALS['manager'])
		{
			Showmsg('该用户是系统管理员，其必须具备后台权限');
		}
		if(!empty($new_password))
		{
			
			if ($edit['username'] == $GLOBALS['manager'])
			{
				Showmsg('您正在试图修改系统管理员的登录密码，此操作请到“系统管理”-“系统管理员”菜单中进行操作。');
			}
			$new_password!=$new_password2 && Showmsg('set_confirmpwd');
			$new_password = md5($new_password);
			$sqladd=",`password`='$new_password'";
		}

		$this->db->update("UPDATE cms_user SET
			`email`='$new_email',
			groupid='$groupid',
			`telephone`='$telephone',
			`idcard`='$idcard',
			`realname`='$realname',
			`ifadmin`='$ifadmin'
			 $sqladd WHERE uid='$uid'
			");
		adminmsg('admin_editok',$this->basename . '&page=' . $_POST['page']);
	}
	
	protected function postMuleditAction()
	{
		if (empty($_POST['userid']))
		{
			showmsg('没有选择任何用户');
		}
		if (empty($_POST['togroup']))
		{
			showmsg('没有选择目标组');
		}
		$userid = array();
		$togroup = intval($_POST['togroup']);
		$page = intval($_POST['page']);
		foreach ($_POST['userid'] as $uid)
		{
			!empty($uid) && array_push($userid, $uid);
		}
		$this->db->query("UPDATE cms_user SET groupid='$togroup' WHERE uid IN(" . implode(',', $userid) . ")");
		adminmsg('批量修改操作成功', $this->basename . '&page=' . $page);
	}
	
	protected function postMuldelAction()
	{
		if (empty($_POST['userid']))
		{
			showmsg('没有选择任何用户');
		}
		$userid = array();
		$page = intval($_POST['page']);
		foreach ($_POST['userid'] as $uid)
		{
			!empty($uid) && array_push($userid, $uid);
		}
		$this->db->query("DELETE FROM cms_user WHERE uid IN(" . implode(',', $userid) . ")");
		adminmsg('批量删除操作成功', $this->basename . '&page=' . $page);		
	}
	
	protected function getDelAction()
	{
		$uid = intval($_GET['uid']);
		$edit = $this->db->get_one("SELECT * FROM cms_user WHERE uid='$uid'");
		if ($edit['username'] == $GLOBALS['manager'])
		{
			Showmsg('该用户是系统管理人员，无法删除');
		}
		$this->db->update("DELETE FROM cms_user WHERE uid='$uid'");
		adminmsg('成功删除了该用户');
	}
	
	protected function getInitAction()
	{
		if (!$this->config['nbc'] || empty($this->config['nbcurl'])) {
			Showmsg('您尚未开启整合协同办公或者没有填写协同办公的地址，无法进行此项操作');
		}
		require_once(R_P . 'require/class_nbc.php');
		$nbc = new NBC();
		list($addNum, $updateNum) = $nbc->init($this->config['nbcurl']);
		adminmsg("用户数据同步成功，共新增 $addNum 个用户，更新了 $updateNum 个用户的资料。");		
	}

	private function getGroup($groupid = '')
	{
		require(D_P . 'data/cache/group.php');
		$groupSelect = '';
		foreach ($group as $val)
		{
			$groupSelect .= "<option value='$val[groupid]'>$val[groupname]</option>";
		}
		if (!empty($groupid))
		{
			$groupSelect = str_replace("value='$groupid'", "value='$groupid' SELECTED", $groupSelect);
		}
		return $groupSelect;
	}
	
	protected function getTotalAction()
	{
		if (!isset($_GET['limit']) || !isset($_GET['id']))
		{
			$id = 0;
			$rs = $this->db->get_one("SELECT COUNT(*) AS total FROM cms_user");
			$limit = $rs['total'];
			$id = 1;
		}
		else 
		{
			$limit = intval($_GET['limit']);
			$id = intval($_GET['id']); 
		}
		
		$id < 1 && $id = 1;
		$start = $id-1;
		$rs = $this->db->get_array("SELECT username,uid FROM cms_user LIMIT $start,50"); //一次50个用户
		foreach ($rs as $r)
		{
			$uid = $r['uid'];
			$username = $r['username'];
			//$rt = $this->db->get_one("select count(*) as total from cms_resource r left join cms_contentindex i using(tid) where i.publisher='$username' AND r.delflag=0");
			$rt = $this->db->get_one("select count(*) as total from cms_contentindex i where i.publisher='$username'");
			$this->db->query("UPDATE cms_user SET postnum=$rt[total] WHERE uid=$uid");
		}
		$id += 50;
		if ($id >= $limit)
		{
			adminmsg('全部重新统计完成', $this->basename);
		}
		else 
		{
			adminmsg('统计了' . $id-1  . '个用户，共计' . $limit . '个用户，现在自动进入下一步', $this->basename . '&action=total&id=' . $id . '&limit=' . $limit);
		}
	}

}

new user();
?>